package org.nch.videoLauncher.presenter;

import org.nch.videoLauncher.task.ProfileTask;
import org.nch.videoLauncher.task.ProfileUtil;
import org.nch.videoLauncher.view.IAddEditProfileView;
import org.nch.videoLauncher.domain.Profile;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Dec 14, 2008
 * Time: 8:47:14 AM
 * To change this template use File | Settings | File Templates.
 */
public class AddEditProfilePresenter {

    private IAddEditProfileView view;
    private ProfileTask task;
    public List<Profile> profiles;
    private MainWindowPresenter mainWindowPresenter;
    private Integer activeProfileId;
    private Integer activeProfileIndex;

    public AddEditProfilePresenter(IAddEditProfileView aepv, MainWindowPresenter mwPresenter){
        this.view = aepv;
        this.mainWindowPresenter = mwPresenter;
        this.task = new ProfileTask();
        this.profiles = task.getAllProfiles();
//        Collections.sort(this.videoItems);

        activeProfileId = ProfileUtil.getProfile().getId();

        for (Profile p : profiles){
            view.addListElement(p.getName());
        }

        view.setDetailsEnabled(false);
    
    }

    public void initialize(){
        this.profiles = task.getAllProfiles();
        view.setDetailsEnabled(false);
    }

    public void selectionChanged(){

        Integer index = view.getCurrentSelection();
        System.out.println("selection index: " + index);

        Profile pf = profiles.get(index);

        view.setDetailName(pf.getName());
        view.setDetailBaseVideoPath(pf.getBaseVideoPath());
        view.setDetailUrlCommand(pf.getUrlCommand());
        view.setDetailVlcCommand(pf.getVlcCommand());

        view.setDetailsEnabled(true);
    }

    public void addProfile(){

        Profile newProfile;

        view.clearDetails();
        if (view.getCurrentSelection() > -1) {       // profile is selected, so use it to copy
            newProfile = profiles.get(view.getCurrentSelection()).copy();
        } else {                                     // no profile selected, so create new profile from scratch
            newProfile = new Profile("New profile - " + (profiles.size() + 1), "Change this value");
        }
        profiles.add(profiles.size(), newProfile);      // add the profile to the end of the list
        view.addListElement(newProfile.getName());      // add it to the view list
        view.setCurrentSelection(profiles.size() - 1);
    }

    public void deleteProfile(){

        Integer index = view.getCurrentSelection();

        activeProfileId = ProfileUtil.getProfile().getId();
        if (activeProfileId.intValue() == profiles.get(index).getId().intValue()){
            return;        // don't delete the active profile
        }        

        view.clearDetails();
        view.setDetailsEnabled(false);

        task.deleteProfile(profiles.get((int)index));
        profiles.remove((int)index);
        view.removeListElement(index);
        view.setCurrentSelection(0); // set the current selection to the top of the list
    }

    public void detailsChanged(){

        activeProfileId = ProfileUtil.getProfile().getId();

        Integer index = view.getCurrentSelection();
        profiles.get(index).setName(view.getDetailName());
        profiles.get(index).setBaseVideoPath(view.getDetailBaseVideoPath());
        profiles.get(index).setUrlCommand(view.getDetailUrlCommand());
        profiles.get(index).setVlcCommand(view.getDetailVlcCommand());
        task.saveProfile(profiles.get(index));
        view.updateListElement(index, profiles.get(index).getName());
        view.setCurrentSelection(view.getCurrentSelection());

        if (activeProfileId.intValue() == profiles.get(index).getId().intValue()){   // current profile has been changed
            ProfileUtil.setActiveProfileId(activeProfileId);   // force profile to reload
        }
        mainWindowPresenter.setProfileList();
    }

}
